package com.oracle.intelagr.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oracle.intelagr.common.JsonResult;
import com.oracle.intelagr.common.PageModel;
import com.oracle.intelagr.entity.BAirmonitoring;
import com.oracle.intelagr.entity.BWatermonitoring;
import com.oracle.intelagr.entity.MMonitorpoint;
import com.oracle.intelagr.entity.User;
import com.oracle.intelagr.service.IAirmoniService;
import com.oracle.intelagr.service.IMonnitorPointService;
import com.oracle.intelagr.service.IWaterMoniService;

@Controller
@RequestMapping("/waterMoni")
public class WaterMoniController {
	@Autowired
	private  IWaterMoniService watermoni;
	//处理查询请求的方法
	@RequestMapping("/list")
	public String list(@DateTimeFormat(pattern="yyyy-MM-dd") Date beginDate,@DateTimeFormat(pattern="yyyy-MM-dd")Date  endDate,PageModel pageModel,Map map) {
		//接受请求参数调用service获得结果将结果传给视图层，向视图层跳转，
		Map paramMap = new HashMap();
		paramMap.put("startMoniorDate", beginDate);
		paramMap.put("endMoniorDate", endDate);
		watermoni.query(pageModel, paramMap);
		map.put("pageModel", pageModel);
		map.put("beginDate", beginDate);
		map.put("endDate", endDate);
		SimpleDateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");
		if(beginDate!=null) {
			map.put("beginDate", dateFormat2.format(beginDate));
		}
		if(endDate!=null) {
			map.put("endDate", dateFormat2.format(endDate));
		}
		return "/watermoni/waterMoniList";
	}
	@Autowired
	private IMonnitorPointService monitorPointService;
	@RequestMapping("/add")
	public String add(Map map) {
		List<MMonitorpoint> queryAll = monitorPointService.queryAll();
		map.put("moniPointList", queryAll);		
		return "/watermoni/waterMoniAdd";
	}
	
	@RequestMapping("/save")
	@ResponseBody
	public JsonResult save(@RequestBody BWatermonitoring bw,HttpServletRequest request,ModelMap map) {
		bw.setDeleteflag("N");
		HttpSession session = request.getSession();
		User user = (User)session.getAttribute("user");
		bw.setCreateuserid(user.getUserID());
		bw.setCreatedate(new Date());
		bw.setUpdateuserid(user.getUserID());
		bw.setUpdatedate(new Date());
		watermoni.save(bw);
		map.put("water", watermoni);
		//respobsebody会把它编程json属性 { status:200,msg:null,data:null}
		return new JsonResult(true);
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public JsonResult delete(@RequestBody int []ids) {
		
		watermoni.delete(ids);
		return new JsonResult(true);
	}
	@RequestMapping("/edit")
	public String edit(int id,Map map) {
		BWatermonitoring queryById = watermoni.queryById(id);
		SimpleDateFormat dateFormat2=new SimpleDateFormat("yyyy-MM-dd");
		String format = dateFormat2.format(queryById.getMonitordate());
		map.put("monidate", format);
		map.put("water", queryById);
		List<MMonitorpoint> queryAll = monitorPointService.queryAll();
		map.put("moniPointList", queryAll);		
		return "/watermoni/waterMoniEdit";
	}
	@RequestMapping("/update")
	@ResponseBody
	public JsonResult update(@RequestBody BWatermonitoring air) {
		watermoni.update(air);
		return new JsonResult(true);
	}
	
}
